package com.mercury.core.exception;

import com.mercury.core.result.code.BaseCode;
import com.mercury.core.result.code.IRespCode;
import lombok.Getter;

import static java.lang.String.format;

/**
 * 接口访问异常
 *
 * @author mercury
 * @date 2020/10/11 2:54 下午
 */
public class MercuryException extends RuntimeException {
    @Getter
    private final String code;

    @Getter
    private final boolean badRequest;


    public MercuryException(IRespCode code, Object... args) {
        this(false, code, args);
    }

    public MercuryException(String message) {
        this(false, message, BaseCode.UNAVAILABLE_FOR_LEGAL_REASONS);
    }

    public MercuryException(String code, String message) {
        this(false, message, code);
    }

    public MercuryException(String message, IRespCode code, Object... args) {
        this(false, message, code, args);
    }

    MercuryException(boolean badRequest, IRespCode code, Object... args) {
        this(badRequest, null, code, args);
    }

    MercuryException(boolean badRequest, String message, String code, Object... args) {
        super(format(message == null ? "Unknown Error" : message, args));
        this.code = code;
        this.badRequest = badRequest;
    }

    MercuryException(boolean badRequest, String message, IRespCode code, Object... args) {
        super(format(message == null ? code.getMessage() : message, args));
        this.code = code.getRespCode();
        this.badRequest = badRequest;
    }
}
